package comp.wxapi.handler;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import comp.wxapi.utils.RequestUtils;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutImageMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMusicMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTransferKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutVideoMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutVoiceMessage;

public class MessageHandler implements WxMpMessageHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandler.class);
	
	private String url;
	
	public MessageHandler(String url) {
		this.url = url;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
			WxSessionManager sessionManager) throws WxErrorException {
		String localUrl = this.url;
		if(!localUrl.startsWith("http")) {
			if(localUrl.startsWith("/")) {
				localUrl = System.getenv("GATEWAY_HOST")+localUrl;
			}else {
				localUrl = System.getenv("GATEWAY_HOST")+"/"+localUrl;
			}
		}
		String res = null;
		try {
			res = RequestUtils.post(localUrl, JSONObject.toJSONString(wxMpService)).string();
		} catch (IOException e) {
			LOGGER.error("error:",e);
		}
		return jsonMsg2WxMpXmlOutMessage(res);
	}
	
	/**
	 * 回复微信事件消息内容转换：json->xml
	 * @param rString
	 * @return
	 */
	private WxMpXmlOutMessage jsonMsg2WxMpXmlOutMessage(String rString) {
		WxMpXmlOutMessage wxMpXmlOutMessage = null;
		//json消息转xml消息回复微信
		if(StringUtils.isNotEmpty(rString)) {
			JSONObject jsonObject = JSON.parseObject(rString);
			String msgType = jsonObject.getString("msgType");
			switch (msgType) {
				case WxConsts.XmlMsgType.TEXT:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutTextMessage.class);
					break;
				case WxConsts.XmlMsgType.IMAGE:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutImageMessage.class);
					break;
				case WxConsts.XmlMsgType.MUSIC:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutMusicMessage.class);
					break;
				case WxConsts.XmlMsgType.NEWS:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutNewsMessage.class);
					break;
				case WxConsts.XmlMsgType.VIDEO:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutVideoMessage.class);
					break;
				case WxConsts.XmlMsgType.VOICE:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutVoiceMessage.class);
					break;
				case WxConsts.XmlMsgType.TRANSFER_CUSTOMER_SERVICE:
					wxMpXmlOutMessage = JSON.parseObject(rString, WxMpXmlOutTransferKefuMessage.class);
					break;
				default:
					break;
			}
		}
		return wxMpXmlOutMessage;
	}
	
	
}
